package org.wutopia.labcloud.domain.uaa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wutopia.labcloud.domain.uaa.controller.vo.output.UserNotificationGetListOutput;
import org.wutopia.labcloud.domain.uaa.controller.vo.output.UserNotificationGetListOutputVO;
import org.wutopia.labcloud.domain.uaa.dao.UserNotificationDao;
import org.wutopia.labcloud.domain.uaa.dao.entity.UserNotification;
import org.wutopia.labcloud.domain.uaa.service.UserNotificationService;
import org.wutopia.labcloud.library.common.base.PageInputVO;
import org.wutopia.labcloud.library.common.base.PageOutputVO;

import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
@AllArgsConstructor
public class UserNotificationServiceImpl implements UserNotificationService {

    private UserNotificationDao userNotificationDao;

    @Override
    public PageOutputVO<UserNotificationGetListOutputVO> getList(PageInputVO vo) {

        Page<UserNotification> page = new Page<>(vo.getPage(), vo.getSize());
        QueryWrapper<UserNotification> q = new QueryWrapper<>();
        q.orderByDesc("create_time");
        Page<UserNotification> userNotificationPage = userNotificationDao.selectPage(page, q);

        PageOutputVO<UserNotificationGetListOutputVO> pageOutputVO = new PageOutputVO<>();
        UserNotificationGetListOutputVO outputVO = new UserNotificationGetListOutputVO();
        List<UserNotification> records = userNotificationPage.getRecords();

        List<UserNotificationGetListOutput> collect = records.stream().map(item -> {
            UserNotificationGetListOutput output = new UserNotificationGetListOutput();

            output.setId(item.getId());
            output.setContent(item.getContent());
            output.setTitle(item.getTitle());
            output.setCreateTime(item.getCreateTime());
            output.setIsRead(item.getIsRead());
            output.setUserId(item.getUserId());
            return output;
        }).collect(Collectors.toList());


        outputVO.setList(collect);

        pageOutputVO.setTotalPage(userNotificationPage.getPages());
        pageOutputVO.setPage(userNotificationPage.getCurrent());
        pageOutputVO.setTotal(userNotificationPage.getTotal());
        pageOutputVO.setSize(userNotificationPage.getSize());
        pageOutputVO.setData(outputVO);
        return pageOutputVO;
    }

    @Override
    public void delete(Long id) {
        userNotificationDao.deleteById(id);
    }


}
